package com.conferenceroom.service.impl;

import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.conferenceroom.dto.AjaxResult;
import com.conferenceroom.dto.CommentAfterOrder;
import com.conferenceroom.entity.Comment;
import com.conferenceroom.entity.Orders;
import com.conferenceroom.mapper.CommentMapper;
import com.conferenceroom.service.CommentService;
import com.conferenceroom.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private OrdersService ordersService;
    @Override
    public IPage<Comment> pageByCondition(IPage<Comment> page, Comment comment) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();

        // 根据 comment 实体中的字段构建查询条件
        if (comment.getCommentId() != null) {
            queryWrapper.eq("comment_id", comment.getCommentId());
        }
        if (comment.getOrderId() != null) {
            queryWrapper.eq("order_id", comment.getOrderId());
        }
        if (comment.getCommentTime() != null) {
            queryWrapper.eq("comment_time", comment.getCommentTime());
        }
        if (comment.getContent() != null && !comment.getContent().isEmpty()) {
            queryWrapper.like("content", comment.getContent());
        }
        if (comment.getRating() != null) {
            queryWrapper.eq("rating", comment.getRating());
        }
        if (comment.getReplyId() != null) {
            queryWrapper.eq("reply_id", comment.getReplyId());
        }
        if (comment.getCommentType() != null) {
            queryWrapper.eq("comment_type", comment.getCommentType());
        }

        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public AjaxResult saveCommentAfterOrder(CommentAfterOrder commentAfterOrder) {
        // 1. 验证订单是否存在且已完成（需要调用OrderService）
        // 1. 验证订单是否存在且已完成
        Orders order = ordersService.getById(commentAfterOrder.getOrderId());
        if (order == null) {
            return AjaxResult.fail("订单不存在");
        }
        if (order.getPaymentStatus() != 6) {
            return AjaxResult.fail("订单未完成，无法评论");
        }
        // 2. 构建评论实体
        Comment comment = new Comment();
        comment.setOrderId(commentAfterOrder.getOrderId());
        comment.setRoomId(commentAfterOrder.getRoomId());
        comment.setContent(commentAfterOrder.getContent());
        comment.setCommentTime(new Date());
        comment.setRating(5); // 默认评分
        comment.setCommentType(1); // 设置评论类型为1级评论

        // 3. 保存评论
        boolean saved = save(comment);

        // 4. 更新订单状态为已评论（如果需要）

        return saved ? AjaxResult.success("评论成功") : AjaxResult.fail("评论失败");
    }
}
